package oa.web.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import oa.core.Constant;
import oa.core.MySession;
import oa.web.pojo.Employee;
import oa.web.service.SalaryService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class Salary {
	
	@Autowired SalaryService salaryService;
	
	@RequestMapping("/personSalary")
	public void personSalary(HttpServletRequest req, HttpServletResponse rep, ModelMap map){
        //System.out.println("balalalalalalalaal&&&&&&&&&&&&&&&");
		double salary = salaryService.getCurrentUserOwnSalary(req);
        PrintWriter printWriter = null;
        try {
            printWriter = rep.getWriter();
            if(salary != Constant.COOKIE_EXPIRE){
                map.addAttribute("personSalary", salary);
            }
            printWriter.write(String.valueOf(salary));
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            printWriter.close();
        }

		//map.addAttribute("personSalary", emp.getSalary());
		//return "mySalary";
	}
	
	@RequestMapping("/depSalary")
	public void depSalary(HttpServletRequest req, HttpServletResponse rep, ModelMap map){
		//Employee emp = this.getEmp(req);
		//List<Employee> empList = salaryService.getByManagerId(emp.getId());
        List<Object> employeesList = salaryService.getCurrentManagerEmps(req);
        PrintWriter printWriter = null;
        try {
            printWriter = rep.getWriter();
            if(employeesList == null){
                printWriter.write(String.valueOf(Constant.COOKIE_EXPIRE));
            }else {
                String jsonEmp = JSON.toJSONString(employeesList,true);
                printWriter.write(jsonEmp);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
//        if(employeesList == null){
//
//        }
		//return "depSalary";
	}
	
	public Employee getEmp(HttpServletRequest req) {
		HttpSession session = MySession.getInstance(req);
		Employee emp = null;
		try{
			emp= (Employee)session.getAttribute("emp");
		}catch(Exception e){
			e.printStackTrace();
		}
		return emp;
	}
}
